﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;

using GeoRef;
using TripLOA.GUI.service;
using TripLOA.WebService;
using TripLOA.WebService.Serializable;

namespace TripLOA.WebService.georef_test_2
{
    /// <summary>
    /// Summary description for WSGeoRef
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class WSGeoRef : System.Web.Services.WebService
    {
        private GeoRefCore core;

        public WSGeoRef()
        {
            core = GeoRefCore.Istance();
        }

        [WebMethod(EnableSession=true)]
        public string getGeoTrip()
        {
            return Serialization.serialize(core.getGeoTrip().Payload[0], typeof(GeoTrip), "json"); ;
        }

        [WebMethod(EnableSession=true)]
        public string getTripByID(int id)
        {
            GeoTrip gt = new GeoTrip(Storage.StorageManager.Instance.GetTripById(50));
            string jsonTrip = GeoSerializer.ToJsonString(typeof(GeoTrip), gt);
            return jsonTrip;
        }

        [WebMethod(EnableSession=true)]
        public List<GeoLoc> geoCoding(string location)
        {
            GeoQuery q = new GeoQuery();
            List<GeoLoc> result = q.geoCoding(location);
            return result;
        }

        [WebMethod(EnableSession=true)]
        public List<List<GeoLoc>> multipleGeoCoding(string[] location)
        {
            List<List<GeoLoc>> toRet = new List<List<GeoLoc>>();
            GeoQuery q = new GeoQuery();

            List<GeoLoc> currentGeocoding = null;
            for (int i = 0; i < location.Length; i++)
            {
                currentGeocoding = q.geoCoding(null, null, null, null, location[i]);
                toRet.Add(currentGeocoding);
            }
            return toRet;
        }




        /*
        [WebMethod(EnableSession=true)]
        public string geoTripTest(string jsonGeoTrip)
        {
            GeoTrip l = (GeoTrip)(GeoSerializer.FromJsonString(typeof(GeoTrip), jsonGeoTrip));
            


            l.Start.Description = "cavolo va bene";
            string toRet = GeoSerializer.ToJsonString(typeof(GeoTrip), l);

            using(StreamWriter sw = new StreamWriter("C:\\pippo.txt"))
            {
                sw.Write(toRet);
            }

            return toRet;
        }
        */

        [WebMethod(EnableSession=true)]
        public string reverseGeoCoding(string _lat, string _lng)
        {
            string lat = _lat.Replace('.', ',');
            string lng = _lng.Replace('.', ',');
            
            //Validate parameters
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(double), value = lat });
            parameters.Add(new WSParameter() { type = typeof(double), value = lng });
            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.ErrorDescription = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), "json");
            }

            return Serialization.serialize
                (core.reverseGeoCoding(double.Parse(lat), double.Parse(lng)), typeof(WSGeoLoc), "json");
        }
    }
}

